package vn.edu.ptithcm.pim.controller;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.ParameterizableViewController;

import vn.edu.ptithcm.pim.constant.UrlConstants;
import vn.edu.ptithcm.pim.dom.News;
import vn.edu.ptithcm.pim.service.NewsService;
import vn.edu.ptithcm.pim.service.UserService;
import vn.edu.ptithcm.pim.util.ParamUtil;

public class ShowHomepageNewsDetailController extends
		ParameterizableViewController {
	private UserService userService;
	private NewsService newsService;
	private LocaleResolver localeResolver;

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> bean = new HashMap<String, Object>();
		long id = ParamUtil.getLong(request, "id", 0);
		bean.put("id", id);
		News news = newsService.findById(id);
		bean.put("news", news);

		String locale = ParamUtil.getString(request, "locale", "");
		if (!locale.isEmpty()) {
			getLocaleResolver()
					.setLocale(request, response, new Locale(locale));
		} else {
			locale = getLocaleResolver().resolveLocale(request).getLanguage();
		}

		request.setAttribute("locale", locale);

		return new ModelAndView(UrlConstants.HOMEPAGE_NEWS_DETAIL, "bean", bean);
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public NewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}

	public LocaleResolver getLocaleResolver() {
		return localeResolver;
	}

	public void setLocaleResolver(LocaleResolver localeResolver) {
		this.localeResolver = localeResolver;
	}

}
